package com.xgq.drink.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgq.drink.entity.FavoriteProduct;
import com.xgq.drink.entity.Product;
import com.xgq.drink.entity.R;
import com.xgq.drink.service.IFavoriteProductService;
import com.xgq.drink.service.IProductService;
import com.xgq.drink.utils.JwtTokenUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgq.drink.utils.RUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "收藏产品模块", description = "收藏产品")
@RestController
@RequestMapping("/favoriteProduct")
public class FavoriteProductController {

    @Autowired
    private IFavoriteProductService favoriteProductService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private IProductService productService;
    /**
     * 添加收藏商品
     */
    @PostMapping("/add")
    public R addFavoriteProduct(@RequestParam Long productId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        if (favoriteProductService.isFavoriteProduct(Long.parseLong(userId), productId)) {
            return RUtils.fail("已收藏");
        }
        FavoriteProduct favoriteProduct = new FavoriteProduct();
        favoriteProduct.setProductId(productId);
        favoriteProduct.setUserId(Long.parseLong(userId));
        favoriteProduct.setCreatedAt(LocalDateTime.now());
        favoriteProduct.setIsDel(0);
        if (favoriteProductService.save(favoriteProduct)) {
            return RUtils.success("收藏成功");
        }else {
            return RUtils.fail("收藏失败");
        }
    }

    /**
     * 删除收藏商品
     */
    @DeleteMapping("/delete")
    public R deleteFavoriteProduct(@RequestParam Long favoriteProductId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        if (favoriteProductService.logicDeleteByUserIdAndFavoriteProductId(Long.parseLong(userId), favoriteProductId)==1) {
            return RUtils.success("删除成功");
        } else {
            return RUtils.fail("删除失败");
        }
    }

    /**
     * 分页查询收藏商品
     */
    @GetMapping("/list")
    public IPage<Product> getFavoriteProductList(@RequestParam(value = "current", defaultValue = "1") int current,
                                                         @RequestParam(value = "size", defaultValue = "10") int size, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        Page<FavoriteProduct> favoritePage = new Page<>(current, size);
        // 查询条件：用户ID匹配且isDel为空
        QueryWrapper<FavoriteProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", Long.parseLong(userId));
        queryWrapper.eq("is_del", 0);
        // 获取收藏商品列表
        IPage<FavoriteProduct> favoriteProductPage = favoriteProductService.page(favoritePage, queryWrapper);

        // 提取收藏商品列表中的商品 ID
        List<Long> productIds = favoriteProductPage.getRecords().stream()
                .map(FavoriteProduct::getProductId)
                .collect(Collectors.toList());

        // 根据商品 ID 查询商品信息
        Page<Product> productPage = new Page<>(current, size);
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.in("product_id", productIds);

        return productService.page(productPage, productQueryWrapper);
    }
}